using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace ColorPicker
{
	public partial class ColorPickerCtrl : UserControl
	{
		Color m_selectedColor = Color.AntiqueWhite;
        ColorWrapper CW;
        Color orig_color;
		public Color SelectedColor
		{
			get { return Color.FromArgb((int)Math.Floor(255f*m_opacity), m_selectedColor);; }
			set
			{
				m_opacity = (float)value.A / 255f;
				m_selectedColor = Color.FromArgb(255, value);
                m_colorWheel.SelectedColor = m_selectedColor;
				m_opacitySlider.Percent = m_opacity;
                m_colorSample.Refresh();
                UpdateInfo();
            }
		}
        public void Reset()
        {
            SelectedColor=orig_color;
        }
		public ColorPickerCtrl(ColorWrapper cw)
		{
			InitializeComponent();
            CW = cw;
			m_colorWheel.SelectedColorChanged += new EventHandler(OnColorWheelSelectionChanged);
			m_opacitySlider.SelectedValueChanged += new EventHandler(OnOpacityValueChanged);
			m_eyedropColorPicker.SelectedColorChanged += new EventHandler(OnEyeDropperSelectionChanged);
			m_colorSample.Paint += new PaintEventHandler(OnColorSamplePaint);
            SelectedColor = cw.color;
            orig_color=cw.color;
        }
		void OnEyeDropperSelectionChanged(object sender, EventArgs e)
		{
			m_colorWheel.SelectedColor = m_eyedropColorPicker.SelectedColor;
		}
		float m_opacity = 1;
		void OnOpacityValueChanged(object sender, EventArgs e)
		{
			m_opacity = Math.Max(0, m_opacitySlider.Percent);
			m_opacity = Math.Min(1, m_opacitySlider.Percent);
			m_colorSample.Refresh();
			UpdateInfo();
		}
		void OnColorWheelSelectionChanged(object sender, EventArgs e)
		{
			Color selcol = m_colorWheel.SelectedColor;
			if (selcol != null && selcol != m_selectedColor)
			{
				m_selectedColor = selcol;
				m_colorSample.Refresh();
			}
			UpdateInfo();
		}
		void UpdateInfo()
		{
			Color c = Color.FromArgb((int)Math.Floor(255f*m_opacity), m_selectedColor);
			string info = string.Format("{0} aRGB({1}, {2}, {3}, {4})", m_colorWheel.SelectedHSLColor.ToString(), c.A, c.R, c.G, c.B);
			m_infoLabel.Text = info;

            if (szabade)
            {
                m_R.Value = c.R;
                m_G.Value = c.G;
                m_B.Value = c.B;
            }
            CW.color = Color.FromArgb((int)Math.Floor(255f * m_opacity), (int)m_R.Value, (int)m_G.Value, (int)m_B.Value);
        }
		void OnColorSamplePaint(object sender, PaintEventArgs e)
		{
			Rectangle r = m_colorSample.ClientRectangle;
			r.Inflate(-4,-4);

			int width = r.Width;
			r.Width /= 2;
			
			Color c = Color.FromArgb((int)Math.Floor(255f*m_opacity), m_selectedColor);
			SolidBrush b = new SolidBrush(c);
			e.Graphics.FillRectangle(b, r);

			r.X += r.Width;

			e.Graphics.FillRectangle(Brushes.White, r);
			c = Color.FromArgb(255, m_selectedColor);
			b = new SolidBrush(c);
			e.Graphics.FillRectangle(b, r);
		}

        private bool szabade = true;
//        private bool szabadeg = true;
//        private bool szabadeb = true;
        private void m_R_ValueChanged(object sender, EventArgs e)
        {
            if (szabade)
            {
                szabade = false;
                SelectedColor = Color.FromArgb((int)Math.Floor(255f * m_opacity), (int)m_R.Value, (int)m_G.Value, (int)m_B.Value);
                szabade = true;
            }
        }
        private void m_G_ValueChanged(object sender, EventArgs e)
        {
            if (szabade)
            {
                szabade = false;
                SelectedColor = Color.FromArgb((int)Math.Floor(255f * m_opacity), (int)m_R.Value, (int)m_G.Value, (int)m_B.Value);
                szabade = true;
            }
        }
        private void m_B_ValueChanged(object sender, EventArgs e)
        {
            if (szabade)
            {
                szabade = false;
                SelectedColor = Color.FromArgb((int)Math.Floor(255f * m_opacity), (int)m_R.Value, (int)m_G.Value, (int)m_B.Value);
                szabade = true;
            }
        }

        private void m_R_Validating(object sender, CancelEventArgs e)
        {
            if (!szabade)
                e.Cancel = true;
        }
    }
}
